940A - Points on the line - CodeForces Solution


brute force greedy sortings *1200

Please click on ads to support us..

Python Code:

n,d=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
Max=0
for i in range (n):
    for j in range (i,n):
        if a[j]-a[i]>d:
            break
        Max=max(Max,j-i+1)
print(n-Max)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define li long long int
#define ppb pop_back
#define ct cout 
#define el "\n"
#define ff first
#define ss second
#define sg string
#define vec vector<li>
#define vep vector<pair<li,li>>
#define mep map<li,li>
#define ves vector<string>
#define isort(n) sort(n.begin(), n.end())
#define dsort(n) sort(n.begin(), n.end(), greater<int>());
#define all(n) n.begin(), n.end()
#define ub upper_bound
#define lb lower_bound
#define maxe *max_element
#define mine *min_element
#define X INT_MAX
#define Y INT_MIN
#define sp " "


void take_input(vector<li> &v , int size){
    for (int i = 0; i <size ; ++i)
    {
        li k;
        cin>>k;
        v.pb(k);
    }
}

void printvec(vector<li> &v ){
for (int i = 0; i < v.size(); ++i)
{
    ct<<v[i]<<" ";
}
ct<<el;
}


//*********************************************************
void pranay_prasoon(){
li n,d;
cin>>n>>d;
vec v;
if (n==1)
{
    ct<<0<<el;
    return;
}
take_input(v,n);
isort(v);
vec count;
for (int i = 0; i < n; ++i)
{
    li ans=1;
    for (int j = n-1; j >i ; --j)
    {
        if (i==j)
        {
            continue;
        }
        if (v[j]-v[i]<=d && v[j]-v[i]>=0)
        {
            
            // ct<<v[i]<<sp<<v[j]<<el;
            ans++;
            // ct<<ans<<el;
            
        }

    }
      count.pb(ans);

}
 // printvec(count);
 ct<<n-maxe(all(count))<<el;











 }
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
    int queries=1;
    // cin >> queries;
    while (queries--)
    {
        pranay_prasoon();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2
320A - Magic Numbers
1658A - Marin and Photoshoot
514A - Chewbaсca and Number
382A - Ksenia and Pan Scales
734B - Anton and Digits
1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String